﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Data;

namespace Runda.CMS.Portal.Workspace
{
    public partial class ContractItemListSale : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    txtContractID.Value = Request.QueryString["ContractID"];
                    BindContractItem(string.Empty);
                }
            }
        }

        private void BindContractItem(string OrderBy)
        {
            ContractBll bll = new ContractBll();
            DataTable dt = bll.GetContractItemTable(" and ContractID = '" + Request.QueryString["ContractID"] + "'", OrderBy);

            gridContractItems.DataSource = dt;
            gridContractItems.DataBind();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindContractItem(string.Empty);
        }

        protected void gridContractItems_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                TextBox txtCount = e.Item.FindControl("txtCount") as TextBox;
                DataRowView dv = e.Item.DataItem as DataRowView;

                if (dv["Type"] != DBNull.Value && dv["Type"] != null && Convert.ToInt32(dv["Type"].ToString()) == 2)
                {
                    txtCount.Enabled = false;
                }

            }
        }

        protected void gridContractItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            if (e.CommandName == "save")
            {
                Guid ItemID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ItemID"].ToString());
                TextBox txtGuaranteeVendor = e.Item.FindControl("txtGuaranteeVendor") as TextBox;
                TextBox txtCount = e.Item.FindControl("txtCount") as TextBox;
                TextBox txtPrice = e.Item.FindControl("txtPrice") as TextBox;
                if (txtGuaranteeVendor == null) return;
                if (txtCount == null) return;
                if (txtPrice == null) return;

                int GuaranteeVendor = 0;
                if (txtGuaranteeVendor.Text.Trim() != string.Empty)
                {
                    if (int.TryParse(txtGuaranteeVendor.Text.Trim(), out GuaranteeVendor))
                    {

                    }
                }

                int Count = 0;
                if (txtCount.Text.Trim() != string.Empty)
                {
                    if (int.TryParse(txtCount.Text.Trim(), out Count))
                    {

                    }
                }

                int Price = 0;
                if (txtPrice.Text.Trim() != string.Empty)
                {
                    if (int.TryParse(txtPrice.Text.Trim(), out Price))
                    {

                    }
                }
                CtrtContractItemBll bll = new CtrtContractItemBll();
                CtrtContractItemEntity entity = bll.Get<CtrtContractItemEntity>(ItemID);
                entity.GuaranteeVendor = GuaranteeVendor;
                entity.Count = Count;
                entity.Price = Price;
                entity.TotalPrice = Count * Price;
                bll.Update(entity);
                BindContractItem(string.Empty);
            }
            if (e.CommandName == "delete")
            {
                Guid ItemID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ItemID"].ToString());
                ContractBll bll = new ContractBll();
                bll.DeleteContractItem(ItemID);
                BindContractItem(string.Empty);
                BindGuaranty();
            }
        }

        protected void gridContractItems_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = " Order by " + e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            BindContractItem(OrderBy);
        }

        private void BindGuaranty()
        {
            if (txtItemID.Value != string.Empty)
            {
                gridGuaranty.DataSource = new CtrtItemGuarantyBll().GetAll<CtrtItemGuarantyEntity>(" and ItemID ='" + txtItemID.Value + "'");
                gridGuaranty.DataBind();
            }
        }

        protected void btnRefreshGuaranty_Click(object sender, EventArgs e)
        {
            BindGuaranty();
        }

        protected void gridGuaranty_DeleteCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            Guid GuarantyID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["GuarantyID"].ToString());

            CtrtItemGuarantyBll bll = new CtrtItemGuarantyBll();
            bll.Delete(new CtrtItemGuarantyEntity(GuarantyID));
            BindGuaranty();

        }
    }
}
